var $add=$("#add_button");

function addDlg() {
    $("#addDlg").modal();
    $.ajax({
        url:"/object/max",
        type:"post",
        success:function(data) {
            $('#add_objectId').val(data+1).attr('readonly', true);
        }
    });
}

function validateAdd() {
    return $("#form_add").validate({
        rules: {
            objectId: {
                required: true,
                digits: true
            },
            objectName: {
                required: true
            },
            abilities: {
                required: true
            },
            objRelationship: {
                required: true
            },
            chara: {
                required: false
            }
        },
        messages: {
            objectId: {
                required: "请输入id",
                digits: "id必须是整数！"
            },
            objectName: {
                required: "请输入名称！"
            },
            abilities: {
                required: "请输入能力！"
            },
            objRelationship: {
                required: "请输入实体关系！"
            }
        }
    });
}

$add.click(function () {
    if(validateAdd().form()) {
        // layer.msg("通过验证！");
        save();
    } else {
        // layer.msg("请检查格式！");
    }
});

function save() {
    var form_data=$("#form_add").serialize();
    var b_data= decodeURIComponent(form_data,true);//防止中文乱码
    var userdata=DataDeal.formToJson(b_data);//转化为json
    $.ajax({
        type: "POST",
        url: "/object/save",
        contentType: "application/json;charset=utf-8",
        data: userdata,
        success: function (data) {
            layer.msg(data);
            reload();
            $('#addDlg').modal('hide').on("hidden", function () {
                $(this).removeData("modal");
            });
            document.getElementById("form_add").reset();//重置表单
        }
    });
}
